package dfbz.线程练习;

public class Make implements Runnable {
    //传人一个包子对象
    private Baozi baozi;

    public Make(Baozi baozi) {
        this.baozi = baozi;
    }

    @Override
    public void run() {
        Integer index = 0;
        while (true) { //只要客户一直吃，就一直做
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //定义包子类型
            //上锁
            synchronized (baozi) {
                if(baozi.getFlag()%2==0) {
                    if (index % 2 == 0) {
                        baozi.setName("肉包子");
                        baozi.setPrice(3.5);
                    } else if (index % 2 == 1) {
                        baozi.setName("菜包子");
                        baozi.setPrice(2.5);
                    } else if (index % 2 == 2) {
                        baozi.setName("白馒头");
                        baozi.setPrice(1.5);
                    }//设置包子的三种类型
                    index++;
                    System.out.println("本店正在制作" + baozi.getPrice() + "的" + baozi.getName());
                    baozi.setFlag(baozi.getFlag() + 1);
                    //开始制作包子 1秒钟制作一个包子
                    try {
                        baozi.notify();
                        baozi.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
